/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.flowform.process.web;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.flowform.process.entity.RepairTaskDistributionProcess;
import com.jeeplus.modules.flowform.process.service.RepairTaskDistributionProcessService;

/**
 * 检修任务承接流程表单Controller
 * @author 刘明星
 * @version 2020-11-09
 */
@RestController
@RequestMapping(value = "/flowform/process/repairTaskDistributionProcess")
public class RepairTaskDistributionProcessController extends BaseController {

	@Autowired
	private RepairTaskDistributionProcessService repairTaskDistributionProcessService;

	@ModelAttribute
	public RepairTaskDistributionProcess get(@RequestParam(required=false) String id) {
		RepairTaskDistributionProcess entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = repairTaskDistributionProcessService.get(id);
		}
		if (entity == null){
			entity = new RepairTaskDistributionProcess();
		}
		return entity;
	}

	/**
	 * 检修任务承接流程表单列表数据
	 */
	@GetMapping("list")
	public AjaxJson list(RepairTaskDistributionProcess repairTaskDistributionProcess, HttpServletRequest request, HttpServletResponse response) {
		Page<RepairTaskDistributionProcess> page = repairTaskDistributionProcessService.findPage(new Page<RepairTaskDistributionProcess>(request, response), repairTaskDistributionProcess);
		return AjaxJson.success().put("page",page);
	}

	@GetMapping("flowableupdata")
	public AjaxJson flowableupdata(String testId,String key,String value) {
		repairTaskDistributionProcessService.flowableupdata(testId,key,value);
		return AjaxJson.success("成功");
	}

	/**
	 * 根据Id获取检修任务承接流程表单数据
	 */
	@GetMapping("queryById")
	public AjaxJson queryById(RepairTaskDistributionProcess repairTaskDistributionProcess) {
		return AjaxJson.success().put("repairTaskDistributionProcess", repairTaskDistributionProcess);
	}

	/**
	 * 保存检修任务承接流程表单
	 */
	@PostMapping("save")
	public AjaxJson save(RepairTaskDistributionProcess repairTaskDistributionProcess, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(repairTaskDistributionProcess);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑保存
		repairTaskDistributionProcessService.save(repairTaskDistributionProcess);//保存
		return AjaxJson.success("保存检修任务承接流程表单成功").put("businessTable", "repair_task_distribution_process").put("businessId", repairTaskDistributionProcess.getId());
	}


	/**
	 * 批量删除检修任务承接流程表单
	 */
	@DeleteMapping("delete")
	public AjaxJson delete(String ids) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			repairTaskDistributionProcessService.delete(new RepairTaskDistributionProcess(id));
		}
		return AjaxJson.success("删除检修任务承接流程表单成功");
	}


}